home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Sources / UAppleEvents.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  69.0 KB  |  2,288 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UAppleEvents.cp
  3. // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UAPPLEEVENTS__
  7. #include "UAppleEvents.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UBUSYCURSOR__
  13. #include "UBusyCursor.h"
  14. #endif
  15.  
  16. #ifndef __UCOREERRORMGR__
  17. #include "UCoreErrorMgr.h"
  18. #endif
  19.  
  20. #ifndef __UCOREUTILITIES__
  21. #include "UCoreUtilities.h"
  22. #endif
  23.  
  24. #ifndef __UDISPATCHER__
  25. #include "UDispatcher.h"
  26. #endif
  27.  
  28. #ifndef __UFAILURE__
  29. #include "UFailure.h"
  30. #endif
  31.  
  32. #ifndef __UFILE__
  33. #include "UFile.h"
  34. #endif
  35.  
  36. #ifndef __ULISTITERATOR__
  37. #include "UListIterator.h"
  38. #endif
  39.  
  40. #ifndef __UMACAPPUTILITIES__
  41. #include "UMacAppUtilities.h"
  42. #endif
  43.  
  44. #ifndef __UMEMORY__
  45. #include "UMemory.h"
  46. #endif
  47.  
  48. #ifndef __USCRIPTING__
  49. #include "UScripting.h"
  50. #endif
  51.  
  52. #ifndef __USTREAM__
  53. #include "UStream.h"
  54. #endif
  55.  
  56. #ifndef __USUBSTITUTION__
  57. #include "USubstitution.h"
  58. #endif
  59.  
  60. // Toolbox
  61.  
  62. #ifndef __AEREGISTRY__
  63. #include <AERegistry.h>
  64. #endif
  65.  
  66. #ifndef __PROCESSES__
  67. #include <Processes.h>
  68. #endif
  69.  
  70. #ifndef __RESOURCES__
  71. #include <Resources.h>
  72. #endif
  73.  
  74. #ifndef __SEGLOAD__
  75. #include <SegLoad.h>
  76. #endif
  77.  
  78. // ANSI
  79.  
  80. #ifndef __STDIO__
  81. #include <stdio.h>
  82. #endif
  83.  
  84. //========================================================================================
  85. // static allocations
  86. //========================================================================================
  87.  
  88. AEIdleUPP TAppleEvent::fgIdleProc;
  89. AEFilterUPP TAppleEvent::fgFilterProc;
  90.  
  91. CAEDesc CAEDesc::fgNullDesc;
  92.  
  93. //========================================================================================
  94. // CLASS TAppleEvent
  95. //========================================================================================
  96. #undef Inherited
  97. #define Inherited TObject
  98.  
  99. #pragma segment AEInitialize
  100. MA_DEFINE_CLASS_M1(TAppleEvent,
  101.                    Inherited);
  102.  
  103. //----------------------------------------------------------------------------------------
  104. // TOSADispatcher::MakeCreateElementEvent: 
  105. //----------------------------------------------------------------------------------------
  106. #pragma segment MAOSLDispatch
  107.  
  108. TAppleEvent* TAppleEvent::MakeCreateElementEvent(AEAddressDesc& targetAddress,
  109.                                                  AESendMode sendMode,
  110.                                                  CAEDesc& targetSpecifier,
  111.                                                  DescType newElementType,
  112.                                                  DescType newElementPosition,
  113.                                                  CAEDesc& initialData,
  114.                                                  CAEDesc& initialProperties)
  115. {
  116.     TAppleEvent * theEvent = new TAppleEvent;
  117.     theEvent->IAppleEvent(kAECoreSuite, kAECreateElement, targetAddress, sendMode);
  118.  
  119.     CTempDesc insertionRecord;
  120.     CTempDesc insertionLocation;
  121.     CTempDesc emptyRecord;
  122.  
  123.     FailInfo fi;
  124.     Try(fi)
  125.     {
  126.  
  127.         // create the insertion record
  128.         insertionRecord.CreateRecord();
  129.         insertionRecord.PutKeyDesc(keyAEObject, targetSpecifier);
  130.         insertionRecord.PutKeyEnum(keyAEPosition, newElementPosition);
  131.         insertionRecord.CoerceDesc(typeInsertionLoc, insertionLocation);
  132.  
  133.         // add the insertion record to the apple event
  134.         theEvent->WriteParameter(keyAEInsertHere, insertionLocation);
  135.  
  136.         // add the class of the item to be created to the event
  137.         theEvent->WriteType(keyAEObjectClass, newElementType);
  138.  
  139.         emptyRecord.CreateRecord();
  140.  
  141.         // add the initial data to the event
  142.         if (initialData.GetDataHandle())
  143.             theEvent->WriteParameter(keyAEData, initialData);
  144.         else
  145.             theEvent->WriteParameter(keyAEData, emptyRecord);
  146.  
  147.         if (initialProperties.GetDataHandle())
  148.             theEvent->WriteParameter(keyAEPropData, initialProperties);
  149.         else
  150.             theEvent->WriteParameter(keyAEPropData, emptyRecord);
  151.  
  152.         fi.Success();
  153.     }
  154.     else                                        // recover
  155.     {
  156.         theEvent = (TAppleEvent *)FreeIfObject(theEvent);
  157.         fi.ReSignal();
  158.     }
  159.  
  160.     return theEvent;
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. // TAppleEvent constructor
  165. //----------------------------------------------------------------------------------------
  166. #pragma segment AEInitialize
  167.  
  168. TAppleEvent::TAppleEvent() :
  169.     fSendingMode(kAENoReply),
  170.     fPriority(kAENormalPriority),
  171.     fTimeoutVal(kAEDefaultTimeout),
  172.     fFreeMessage(FALSE)
  173. {
  174.     fMessage.dataHandle = NULL;
  175.  
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // TAppleEvent::IAppleEvent:
  180. //----------------------------------------------------------------------------------------
  181. #pragma segment AEInitialize
  182.  
  183. void TAppleEvent::IAppleEvent(const AEEventClass itsEventClass,
  184.                               const AEEventID itsEventID,
  185.                               const AEAddressDesc& itsAddress,
  186.                               long itsSendingMode)
  187. {
  188.     AppleEvent theMessage;
  189.  
  190.     IObject();
  191.  
  192.     FailInfo fi;
  193.     Try(fi)
  194.     {
  195.         FailOSErr(AECreateAppleEvent(itsEventClass, itsEventID, &itsAddress, kAutoGenerateReturnID, kAnyTransactionID, &theMessage));
  196.         fi.Success();
  197.     }
  198.     else                                        // Recover
  199.     {
  200.         Free();
  201.         fi.ReSignal();
  202.     }
  203.  
  204.     fMessage = theMessage;
  205.     fSendingMode = itsSendingMode;
  206.     fFreeMessage = TRUE;                        // We created it, we need to free it
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // TAppleEvent::InitializeFromMessage:
  211. //----------------------------------------------------------------------------------------
  212. #pragma segment AEInitialize
  213.  
  214. void TAppleEvent::InitializeFromMessage(const AppleEvent& theMessage,
  215.                                         Boolean freeMessage)
  216. {
  217.     IObject();
  218.  
  219.     fMessage = theMessage;
  220.     fFreeMessage = freeMessage;
  221.  
  222.     // Initialize the reply mode.
  223.     DescType actualType;
  224.     Size actualSize;
  225.     short interactLevel = 0;
  226.     if (AEGetAttributePtr(&theMessage, keyInteractLevelAttr, typeShortInteger, &actualType, (Ptr) & interactLevel, sizeof(short), &actualSize) == noErr)
  227.         fSendingMode = (interactLevel & kAEReplyModeMask);
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // TAppleEvent::Free:
  232. //----------------------------------------------------------------------------------------
  233. #pragma segment AEClose
  234.  
  235. TAppleEvent::~TAppleEvent()
  236. {
  237.     if (fFreeMessage)
  238.         AEDisposeDesc(&fMessage);
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. // TAppleEvent::Send:
  243. //----------------------------------------------------------------------------------------
  244. #pragma segment AESend
  245.  
  246. TAppleEvent* TAppleEvent::Send()
  247. {
  248.     TAppleEvent * resultEvent = NULL;
  249.     FailOSErr(SendEvent(resultEvent));
  250.  
  251.     return resultEvent;
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. // TAppleEvent::SendEvent:
  256. //----------------------------------------------------------------------------------------
  257. #pragma segment AESend
  258.  
  259. OSErr TAppleEvent::SendEvent()
  260. {
  261.     TAppleEvent * resultEvent = NULL;
  262.     OSErr err = SendEvent(resultEvent);
  263.     FreeIfObject(resultEvent);
  264.  
  265.     return err;
  266. }
  267.  
  268. //----------------------------------------------------------------------------------------
  269. // TAppleEvent::SendEvent:
  270. //----------------------------------------------------------------------------------------
  271. #pragma segment AESend
  272.  
  273. OSErr TAppleEvent::SendEvent(TAppleEvent*& replyEvent)
  274. {
  275.     AppleEvent reply;
  276.  
  277.     OSErr err = AECreateDesc(typeNull, NULL, 0, &reply);
  278.     if (err != noErr)
  279.         return err;
  280.  
  281.     // Debugging note: 
  282.     //    If the application is sending this Apple Event to itself, you can follow it
  283.     //    through the AESend by setting breakpoints in the functions
  284.     //    TOSADispatcher::PreDispatchHandlerGlue and TOSADispatcher::DispatchHandlerGlue
  285.     //    in UScripting.cp. 
  286.  
  287.     Boolean keepBusy;
  288.     if (gBusyCursor)
  289.         keepBusy = gBusyCursor->KeepCursorBusy(TRUE);
  290.  
  291.     err = AESend(&fMessage, &reply, fSendingMode, fPriority, fTimeoutVal, TAppleEvent::fgIdleProc, TAppleEvent::fgFilterProc);
  292.  
  293.     if (gBusyCursor)
  294.         gBusyCursor->KeepCursorBusy(keepBusy);
  295.  
  296.     if ((err == noErr) && (GetSendingMode() & kAEReplyModeMask) == kAEWaitReply)
  297.     {
  298.         replyEvent = new TAppleEvent;
  299.         replyEvent->InitializeFromMessage(reply, TRUE);
  300.     }
  301.     else
  302.         AEDisposeDesc(&reply);
  303.  
  304.     return err;
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. // TAppleEvent::GetAddress:
  309. //----------------------------------------------------------------------------------------
  310. #pragma segment AEAttributes
  311.  
  312. void TAppleEvent::GetAddress(AEAddressDesc& theAddress)
  313.  
  314. {
  315.     FailOSErr(AEGetAttributeDesc(&fMessage, keyAddressAttr, typeWildCard, &theAddress));
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. // TAppleEvent::GetProcessSerialNumber:
  320. //----------------------------------------------------------------------------------------
  321. #pragma segment AEAttributes
  322.  
  323. void TAppleEvent::GetProcessSerialNumber(ProcessSerialNumber& thePSN)
  324.  
  325. {
  326.     DescType actualType;
  327.     Size actualSize;
  328.     FailOSErr(AEGetAttributePtr(&fMessage, keyAddressAttr, typeProcessSerialNumber, &actualType, &thePSN, sizeof(ProcessSerialNumber), &actualSize));
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------
  332. // TAppleEvent::GetReturnID:
  333. //----------------------------------------------------------------------------------------
  334. #pragma segment AEAttributes
  335.  
  336. long TAppleEvent::GetReturnID()
  337. {
  338.     DescType actualType;
  339.     long returnID;
  340.     long actualSize;
  341.  
  342.     FailOSErr(AEGetAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &actualType, &returnID, sizeof(returnID), &actualSize));
  343.  
  344.     return returnID;
  345. }
  346.  
  347. //----------------------------------------------------------------------------------------
  348. // TAppleEvent::GetTransactionID:
  349. //----------------------------------------------------------------------------------------
  350. #pragma segment AEAttributes
  351.  
  352. long TAppleEvent::GetTransactionID()
  353. {
  354.     long transactionID;
  355.     DescType actualType;
  356.     long actualSize;
  357.  
  358.     return (AEGetAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &actualType, &transactionID, sizeof(transactionID), &actualSize) == noErr) ? transactionID : 0;
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // TAppleEvent::SetAddress:
  363. //----------------------------------------------------------------------------------------
  364. #pragma segment AEAttributes
  365.  
  366. void TAppleEvent::SetAddress(const AEAddressDesc& theAddress)
  367. {
  368.     FailOSErr(AEPutAttributeDesc(&fMessage, keyAddressAttr, &theAddress));
  369. }
  370.  
  371. //----------------------------------------------------------------------------------------
  372. // TAppleEvent::SetPriority:
  373. //----------------------------------------------------------------------------------------
  374. #pragma segment AEAttributes
  375.  
  376. void TAppleEvent::SetPriority(short thePriority)
  377. {
  378.     fPriority = thePriority;
  379. }
  380.  
  381. //----------------------------------------------------------------------------------------
  382. // TAppleEvent::SetReturnID:
  383. //----------------------------------------------------------------------------------------
  384. #pragma segment AEAttributes
  385.  
  386. void TAppleEvent::SetReturnID(long returnID)
  387. {
  388.     FailOSErr(AEPutAttributePtr(&fMessage, keyReturnIDAttr, typeLongInteger, &returnID, sizeof(returnID)));
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. // TAppleEvent::SetSendingMode:
  393. //----------------------------------------------------------------------------------------
  394. #pragma segment AEAttributes
  395.  
  396. void TAppleEvent::SetSendingMode(long sendingMode)
  397. {
  398.     fSendingMode = sendingMode;
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. // TAppleEvent::SetTimeoutVal:
  403. //----------------------------------------------------------------------------------------
  404. #pragma segment AEAttributes
  405.  
  406. void TAppleEvent::SetTimeoutVal(long theTimeoutVal)
  407. {
  408.     fTimeoutVal = theTimeoutVal;
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. // TAppleEvent::SetTransactionID:
  413. //----------------------------------------------------------------------------------------
  414. #pragma segment AEAttributes
  415.  
  416. void TAppleEvent::SetTransactionID(long transactionID)
  417. {
  418.     FailOSErr(AEPutAttributePtr(&fMessage, keyTransactionIDAttr, typeLongInteger, &transactionID, sizeof(transactionID)));
  419. }
  420.  
  421. //----------------------------------------------------------------------------------------
  422. // TAppleEvent::ReadEventClass:
  423. //----------------------------------------------------------------------------------------
  424. #pragma segment AEReadWrite
  425.  
  426. AEEventClass TAppleEvent::ReadEventClass()
  427. {
  428.     DescType typeCode;
  429.     AEEventClass eventClass;
  430.     Size actualSize;
  431.  
  432.     FailOSErr(AEGetAttributePtr(&fMessage, keyEventClassAttr, typeType, &typeCode, &eventClass, sizeof(eventClass), &actualSize));
  433.     return eventClass;
  434. }
  435.  
  436. //----------------------------------------------------------------------------------------
  437. // TAppleEvent::ReadEventClass:
  438. //----------------------------------------------------------------------------------------
  439. #pragma segment AEReadWrite
  440.  
  441. AEEventID TAppleEvent::ReadEventID()
  442. {
  443.     DescType typeCode;
  444.     AEEventID eventID;
  445.     Size actualSize;
  446.  
  447.     FailOSErr(AEGetAttributePtr(&fMessage, keyEventIDAttr, typeType, &typeCode, &eventID, sizeof(eventID), &actualSize));
  448.     return eventID;
  449. }
  450.  
  451. //----------------------------------------------------------------------------------------
  452. // TAppleEvent::ReadShort:
  453. //----------------------------------------------------------------------------------------
  454. #pragma segment AEReadWrite
  455.  
  456. short TAppleEvent::ReadShort(const AEKeyword theKey)
  457. {
  458.     DescType actualType;
  459.     Size actualSize;
  460.     short theData = 0;
  461.  
  462.     FailOSErr(AEGetParamPtr(&fMessage, theKey, typeShortInteger, &actualType, &theData, sizeof(theData), &actualSize));
  463.  
  464.     return theData;
  465. }
  466.  
  467. //----------------------------------------------------------------------------------------
  468. // TAppleEvent::ReadLong:
  469. //----------------------------------------------------------------------------------------
  470. #pragma segment AEReadWrite
  471.  
  472. long TAppleEvent::ReadLong(const AEKeyword theKey)
  473. {
  474.     DescType actualType;
  475.     Size actualSize;
  476.     long theData = 0;
  477.  
  478.     FailOSErr(AEGetParamPtr(&fMessage, theKey, typeLongInteger, &actualType, &theData, sizeof(theData), &actualSize));
  479.  
  480.     return theData;
  481. }
  482.  
  483. //----------------------------------------------------------------------------------------
  484. // TAppleEvent::ReadString:
  485. //----------------------------------------------------------------------------------------
  486. #pragma segment AEReadWrite
  487.  
  488. void TAppleEvent::ReadString(const AEKeyword theKey,
  489.                              CStr255& theData)
  490. {
  491.     DescType actualType;
  492.     Size actualSize;
  493.  
  494.     theData.Empty();
  495.     FailOSErr(AEGetParamPtr(&fMessage, theKey, typeChar, &actualType, &theData[1], sizeof(CStr255) - 1, &actualSize));
  496.  
  497.     if (actualSize > 255)
  498.     {
  499. #if qDebug
  500.         fprintf(stderr, "TAppleEvent::ReadString: String was longer than 255 bytes.  Will be truncated\n");
  501. #endif
  502.  
  503.         actualSize = 255;
  504.     }
  505.     theData.Length() = (unsigned char)actualSize;
  506. }
  507.  
  508. //----------------------------------------------------------------------------------------
  509. // TAppleEvent::ReadSectionHandle:
  510. //----------------------------------------------------------------------------------------
  511. #pragma segment AEReadWrite
  512.  
  513. SectionHandle TAppleEvent::ReadSectionHandle(const AEKeyword theKey)
  514. {
  515.     DescType actualType;
  516.     Size actualSize;
  517.     SectionHandle theData = NULL;
  518.  
  519.     FailOSErr(AEGetParamPtr(&fMessage, theKey, typeSectionH, &actualType, &theData, sizeof(SectionHandle), &actualSize));
  520.  
  521.     return theData;
  522. }
  523.  
  524. //----------------------------------------------------------------------------------------
  525. // TAppleEvent::ReadPtrList:
  526. //----------------------------------------------------------------------------------------
  527. #pragma segment AEReadWrite
  528.  
  529. void TAppleEvent::ReadPtrList(const AEKeyword theKey,
  530.                               const DescType theType,
  531.                               TDynamicArray* theData)
  532. {
  533.     AEDescList theDescList;
  534.  
  535.     FailOSErr(AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList));
  536.  
  537.     long items;
  538.     AEKeyword theActualKey;
  539.     DescType theActualType;
  540.     Size theActualSize;
  541.     MAVolatileInit(Ptr, thePtr, NULL);
  542.  
  543.     FailInfo fi;
  544.     Try(fi)
  545.     {
  546.         FailOSErr(AECountItems(&theDescList, &items));// Get the number of elements in the list 
  547.         thePtr = NewPermPtr(theData->fElementSize);
  548.  
  549.         for (short index = 1; index <= items; ++index)
  550.         {
  551.             FailOSErr(AEGetNthPtr(&theDescList, index, theType, &theActualKey, &theActualType, thePtr, theData->fElementSize, &theActualSize));
  552.  
  553.             theData->InsertElementsBefore(theData->GetSize() + 1, thePtr, 1);
  554.         }
  555.         fi.Success();
  556.     }
  557.     else                                        // Recover
  558.     {
  559.         AEDisposeDesc(&theDescList);
  560.         thePtr = DisposeIfPtr(thePtr);
  561.         fi.ReSignal();
  562.     }
  563.  
  564.     AEDisposeDesc(&theDescList);
  565.     thePtr = DisposeIfPtr(thePtr);
  566. }
  567.  
  568. //----------------------------------------------------------------------------------------
  569. // TAppleEvent::ReadHandleList:
  570. //----------------------------------------------------------------------------------------
  571. #pragma segment AEReadWrite
  572.  
  573. void TAppleEvent::ReadHandleList(const AEKeyword theKey,
  574.                                  const DescType theType,
  575.                                  TDynamicArray* theData)
  576. {
  577.     AEDescList theDescList;
  578.  
  579.     FailOSErr(AEGetParamDesc(&fMessage, theKey, typeAEList, &theDescList));
  580.  
  581.     AEDesc theHandle;
  582.     AEKeyword theActualKey;
  583.     long items;
  584.     FailInfo fi;
  585.  
  586.     Try(fi)
  587.     {
  588.         theHandle.dataHandle = NULL;
  589.         FailOSErr(AECountItems(&theDescList, &items));// Get the number of elements in the list 
  590.         for (short index = 1; index <= items; ++index)
  591.         {
  592.             FailOSErr(AEGetNthDesc(&theDescList, index, theType, &theActualKey, &theHandle));
  593.             theData->InsertElementsBefore(theData->GetSize() + 1, &theHandle.dataHandle, 1);
  594.             theHandle.dataHandle = NULL;
  595.         }
  596.         fi.Success();
  597.     }
  598.     else
  599.     {
  600.         AEDisposeDesc(&theDescList);
  601.         AEDisposeDesc(&theHandle);
  602.         fi.ReSignal();
  603.     }
  604.     AEDisposeDesc(&theDescList);
  605. }
  606.  
  607. //----------------------------------------------------------------------------------------
  608. // TAppleEvent::ReadParameter:
  609. //----------------------------------------------------------------------------------------
  610. #pragma segment AEReadWrite
  611.  
  612. OSErr TAppleEvent::ReadParameter(const AEKeyword theKey,
  613.                                  const DescType desiredType,
  614.                                  CAEDesc& theData)
  615. {
  616.     return AEGetParamDesc(&fMessage, theKey, desiredType, theData);
  617. }
  618.  
  619. //----------------------------------------------------------------------------------------
  620. // TAppleEvent::ReadParameterPtr:
  621. //----------------------------------------------------------------------------------------
  622. #pragma segment AEReadWrite
  623.  
  624. void TAppleEvent::ReadParameterPtr(const AEKeyword theKey,
  625.                                    const DescType desiredType,
  626.                                    DescType& actualType,
  627.                                    Ptr theData,
  628.                                    long maximumSize,
  629.                                    long& actualSize)
  630. {
  631.     FailOSErr(AEGetParamPtr(&fMessage, theKey, desiredType, &actualType, theData, maximumSize, &actualSize));
  632. }
  633.  
  634. //----------------------------------------------------------------------------------------
  635. // TAppleEvent::WriteShort:
  636. //----------------------------------------------------------------------------------------
  637. #pragma segment AEReadWrite
  638.  
  639. void TAppleEvent::WriteShort(const AEKeyword theKey,
  640.                              short theData)
  641. {
  642.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeShortInteger, &theData, sizeof(theData)));
  643. }
  644.  
  645. //----------------------------------------------------------------------------------------
  646. // TAppleEvent::WriteLong:
  647. //----------------------------------------------------------------------------------------
  648. #pragma segment AEReadWrite
  649.  
  650. void TAppleEvent::WriteLong(const AEKeyword theKey,
  651.                             long theData)
  652. {
  653.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeLongInteger, &theData, sizeof(theData)));
  654. }
  655.  
  656. //----------------------------------------------------------------------------------------
  657. // TAppleEvent::WriteString:
  658. //----------------------------------------------------------------------------------------
  659. #pragma segment AEReadWrite
  660.  
  661. void TAppleEvent::WriteString(const AEKeyword theKey,
  662.                               const CStr255& theData)
  663. {
  664.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeChar, &theData[1], theData.Length()));
  665. }
  666.  
  667. //----------------------------------------------------------------------------------------
  668. // TAppleEvent::WriteSectionHandle:
  669. //----------------------------------------------------------------------------------------
  670. #pragma segment AEReadWrite
  671.  
  672. void TAppleEvent::WriteSectionHandle(const AEKeyword theKey,
  673.                                      SectionHandle theData)
  674. {
  675.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeSectionH, &theData, sizeof(SectionHandle)));
  676. }
  677.  
  678. //----------------------------------------------------------------------------------------
  679. // TAppleEvent::WritePtrList:
  680. //----------------------------------------------------------------------------------------
  681. #pragma segment AEReadWrite
  682.  
  683. void TAppleEvent::WritePtrList(const AEKeyword theKey,
  684.                                const DescType theType,
  685.                                TDynamicArray* theData)
  686. {
  687.     CAEDesc theDescList;                        // used to be VOLATILE, but since it's a structure it won't be cached into registers on OUR compilers
  688.  
  689.     FailInfo fi;
  690.     Try(fi)
  691.     {
  692.         theDescList.CreateList();
  693.  
  694.         {
  695.             // Create a block for the iterator to disambiguate it from the failure handler it's nested in
  696.             CArrayIterator iter(theData);
  697.  
  698.             for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  699.                 FailOSErr(AEPutPtr(theDescList, (long)i, theType, theData->ComputeAddress(i), theData->fElementSize));
  700.         }
  701.         FailOSErr(AEPutParamDesc(&fMessage, theKey, theDescList));
  702.         fi.Success();
  703.     }
  704.     else                                        // Recover
  705.     {
  706.         FailOSErr(theDescList.DisposeDesc());
  707.         fi.ReSignal();
  708.     }
  709.  
  710.     FailOSErr(theDescList.DisposeDesc());
  711. }
  712.  
  713. //----------------------------------------------------------------------------------------
  714. // TAppleEvent::WriteHandleList:
  715. //----------------------------------------------------------------------------------------
  716. #pragma segment AEReadWrite
  717.  
  718. void TAppleEvent::WriteHandleList(const AEKeyword theKey,
  719.                                   const DescType theType,
  720.                                   TDynamicArray* theData)
  721. {
  722.     CAEDesc theDescList;                        // used to be VOLATILE, but since it's a structure it won't be cached into registers on OUR compilers
  723.  
  724.     FailInfo fi;
  725.     Try(fi)
  726.     {
  727.         theDescList.CreateList();
  728.  
  729.         {
  730.             // Create a block for the iterator to disambiguate it from the failure handler it's nested in
  731.             CArrayIterator iter(theData);
  732.  
  733.             for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
  734.             {
  735.                 CAEDesc theElement(theType, NULL);
  736.                 theElement.fAEDesc.dataHandle = *((Handle *)(theData->ComputeAddress(i)));
  737.                 theDescList.PutListItem((long)i, theElement);
  738.             }
  739.         }
  740.  
  741.         FailOSErr(AEPutParamDesc(&fMessage, theKey, theDescList));
  742.         fi.Success();
  743.     }
  744.     else                                        // Recover
  745.     {
  746.         FailOSErr(theDescList.DisposeDesc());
  747.         fi.ReSignal();
  748.     }
  749.  
  750.     FailOSErr(theDescList.DisposeDesc());
  751. }
  752.  
  753. //----------------------------------------------------------------------------------------
  754. // TAppleEvent::WriteParameter:
  755. //----------------------------------------------------------------------------------------
  756. #pragma segment AEReadWrite
  757.  
  758. void TAppleEvent::WriteParameter(const AEKeyword theKey,
  759.                                  const CAEDesc& theData)
  760. {
  761.     FailOSErr(AEPutParamDesc(&fMessage, theKey, theData));
  762. }
  763.  
  764. //----------------------------------------------------------------------------------------
  765. // TAppleEvent::WriteParameterPtr:
  766. //----------------------------------------------------------------------------------------
  767. #pragma segment AEReadWrite
  768.  
  769. void TAppleEvent::WriteParameterPtr(const AEKeyword theKey,
  770.                                     const DescType typeCode,
  771.                                     Ptr theData,
  772.                                     long dataSize)
  773. {
  774.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeCode, theData, dataSize));
  775. }
  776.  
  777. //----------------------------------------------------------------------------------------
  778. // TAppleEvent::DeleteParameter:
  779. //----------------------------------------------------------------------------------------
  780. #pragma segment AEReadWrite
  781.  
  782. void TAppleEvent::DeleteParameter(const AEKeyword theKey)
  783. {
  784.     FailOSErr(AEDeleteKeyDesc(&fMessage, theKey));
  785. }
  786.  
  787. //----------------------------------------------------------------------------------------
  788. // TAppleEvent::CheckRequiredParameters:
  789. //----------------------------------------------------------------------------------------
  790. #pragma segment AEReadWrite
  791.  
  792. void TAppleEvent::CheckRequiredParameters()
  793. {
  794.     // look for the keyMissedKeywordAttr
  795.     DescType returnedType;
  796.     Size actualSize;
  797.     OSErr theErr = AEGetAttributePtr(&fMessage, keyMissedKeywordAttr, typeWildCard, &returnedType, NULL, 0, &actualSize);
  798.     if (theErr == noErr)
  799.     {
  800.         // We missed a parameter!
  801.         FailOSErr(errAEParamMissed);
  802.     }
  803.     else if (theErr != errAEDescNotFound)
  804.     {
  805.         // Its OK if we got errAEDescNotFound because that means we got all the params.
  806.         FailOSErr(theErr);
  807.     }
  808. }
  809.  
  810. //----------------------------------------------------------------------------------------
  811. // TAppleEvent::HasParameter:
  812. //----------------------------------------------------------------------------------------
  813. #pragma segment AEReadWrite
  814.  
  815. Boolean TAppleEvent::HasParameter(const AEKeyword theKey)
  816. {
  817.     DescType typeCode;
  818.     long actualSize;
  819.     return AEGetParamPtr(&fMessage, theKey, typeWildCard, &typeCode, NULL, 0, &actualSize) == noErr;
  820. }
  821.  
  822. //----------------------------------------------------------------------------------------
  823. // TAppleEvent::ParameterType:
  824. //----------------------------------------------------------------------------------------
  825. #pragma segment AEReadWrite
  826.  
  827. DescType TAppleEvent::ParameterType(const AEKeyword theKey)
  828. {
  829.     // Returns the descriptor type of an event parameter.
  830.     // TAppleEvent::ParameterType is here courtesy of Matt Clark. Thanks Matt.
  831.     CTempDesc paramDesc;
  832.  
  833.     OSErr theErr = AEGetParamDesc(&fMessage, theKey, typeWildCard, paramDesc);
  834.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  835.         FailOSErr(theErr);
  836.     DescType result = paramDesc.GetDescriptorType();
  837.     return result;
  838. }
  839.  
  840. //----------------------------------------------------------------------------------------
  841. // TAppleEvent::ReadType:
  842. //----------------------------------------------------------------------------------------
  843. #pragma segment AEReadWrite
  844.  
  845. DescType TAppleEvent::ReadType(const AEKeyword theKey)
  846. {
  847.     DescType actualType;
  848.     long actualSize;
  849.     DescType theData = 0;
  850.  
  851.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeType, &actualType, &theData, sizeof(theData), &actualSize);
  852.  
  853.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  854.         FailOSErr(theErr);
  855.  
  856.     return theData;
  857. }
  858.  
  859. //----------------------------------------------------------------------------------------
  860. // TAppleEvent::ReadEnum:
  861. //----------------------------------------------------------------------------------------
  862. #pragma segment AEReadWrite
  863.  
  864. DescType TAppleEvent::ReadEnum(const AEKeyword theKey)
  865. {
  866.     DescType actualType;
  867.     long actualSize;
  868.     DescType theData = 0;
  869.  
  870.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeEnumeration, &actualType, &theData, sizeof(theData), &actualSize);
  871.  
  872.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  873.         FailOSErr(theErr);
  874.  
  875.     return theData;
  876. }
  877.  
  878. //----------------------------------------------------------------------------------------
  879. // TAppleEvent::ReadBoolean:
  880. //----------------------------------------------------------------------------------------
  881. #pragma segment AEReadWrite
  882.  
  883. Boolean TAppleEvent::ReadBoolean(const AEKeyword theKey)
  884. {
  885.     DescType actualType;
  886.     long actualSize;
  887.     Boolean theData = FALSE;
  888.  
  889.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeBoolean, &actualType, &theData, sizeof(theData), &actualSize);
  890.  
  891.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  892.         FailOSErr(theErr);
  893.  
  894.     return theData;
  895. }
  896.  
  897. //----------------------------------------------------------------------------------------
  898. // TAppleEvent::ReadRect:
  899. //----------------------------------------------------------------------------------------
  900. #pragma segment AEReadWrite
  901.  
  902. void TAppleEvent::ReadRect(const AEKeyword theKey,
  903.                            CRect& theRect)
  904. {
  905.     DescType actualType;
  906.     long actualSize;
  907.     CRect localRect;
  908.  
  909.     OSErr theErr = AEGetParamPtr(&fMessage, theKey, typeQDRectangle, &actualType, &localRect, sizeof(localRect), &actualSize);
  910.  
  911.     if ((theErr != noErr) && (theErr != errAEDescNotFound))
  912.         FailOSErr(theErr);
  913.  
  914.     theRect = localRect;
  915. }
  916.  
  917. //----------------------------------------------------------------------------------------
  918. // TAppleEvent::WriteType:
  919. //----------------------------------------------------------------------------------------
  920. #pragma segment AEReadWrite
  921.  
  922. void TAppleEvent::WriteType(const AEKeyword theKey,
  923.                             DescType theType)
  924. {
  925.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeType, &theType, sizeof(theType)));
  926. }
  927.  
  928. //----------------------------------------------------------------------------------------
  929. // TAppleEvent::WriteEnum:
  930. //----------------------------------------------------------------------------------------
  931. #pragma segment AEReadWrite
  932.  
  933. void TAppleEvent::WriteEnum(const AEKeyword theKey,
  934.                             DescType theEnum)
  935. {
  936.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeEnumeration, &theEnum, sizeof(theEnum)));
  937. }
  938.  
  939. //----------------------------------------------------------------------------------------
  940. // TAppleEvent::WriteBoolean:
  941. //----------------------------------------------------------------------------------------
  942. #pragma segment AEReadWrite
  943.  
  944. void TAppleEvent::WriteBoolean(const AEKeyword theKey,
  945.                                Boolean theBoolean)
  946. {
  947.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeBoolean, &theBoolean, sizeof(theBoolean)));
  948. }
  949.  
  950. //----------------------------------------------------------------------------------------
  951. // TAppleEvent::WriteRect:
  952. //----------------------------------------------------------------------------------------
  953. #pragma segment AEReadWrite
  954.  
  955. void TAppleEvent::WriteRect(const AEKeyword theKey,
  956.                             const CRect& theRect)
  957. {
  958.     FailOSErr(AEPutParamPtr(&fMessage, theKey, typeQDRectangle, &theRect, sizeof(theRect)));
  959. }
  960.  
  961. //----------------------------------------------------------------------------------------
  962. // TAppleEvent::WriteReplySpecifier:
  963. //----------------------------------------------------------------------------------------
  964. #pragma segment MAOSLDispatch
  965.  
  966. Boolean TAppleEvent::WriteReplySpecifier(MScriptableObject* theReplyObject)
  967. {
  968.     // Use this whenever you need to return the object's specifier as the result of an event.
  969.     // Makes an object specifier for an object and puts it in the keyAEResult parameter of this reply event.
  970.     // MScriptableObject::ReturnReplySpecifier is here courtesy of Matt Clark. Thanks Matt.
  971.     Boolean result = FALSE;
  972.  
  973.     if (fMessage.dataHandle)
  974.     {
  975.         CTempDesc objSpecifier;
  976.         if (theReplyObject->MakeObjectSpecifier(objSpecifier, theReplyObject->GetSpecifierForm()))
  977.             WriteParameter(keyAEResult, objSpecifier);
  978.  
  979.         result = TRUE;
  980.     }
  981.  
  982.     return result;
  983. }
  984.  
  985. //----------------------------------------------------------------------------------------
  986. // TAppleEvent::WriteError:
  987. //----------------------------------------------------------------------------------------
  988. #pragma segment AEReadWrite
  989.  
  990. void TAppleEvent::WriteError(long errNum,
  991.                              short errorStringResource,
  992.                              short errorStringIndex)
  993. {
  994.     if (fMessage.dataHandle)
  995.     {
  996.         WriteLong(keyErrorNumber, errNum);
  997.         CStr255 errStr;
  998.         GetIndString(errStr, errorStringResource, errorStringIndex);
  999.         WriteString(keyErrorString, errStr);
  1000.     }
  1001. }
  1002.  
  1003. //----------------------------------------------------------------------------------------
  1004. // TAppleEvent::WriteOSError
  1005. //----------------------------------------------------------------------------------------
  1006. #pragma segment AEReadWrite
  1007.  
  1008. void TAppleEvent::WriteOSError(OSErr error)
  1009. {
  1010.     if (fMessage.descriptorType != typeNull && fMessage.dataHandle)
  1011.     {
  1012.         CStr255 errStr;
  1013.         WriteLong(keyErrorNumber, errAEEventFailed);
  1014.         if (!LookupErrString(error, errReasonID, errStr))
  1015.         {
  1016.             // No error string was found, substitute the generic one.
  1017.             CStr255 errNumStr;
  1018.             LookupErrString(errGenericErrorWithNum, errReasonID, errStr);
  1019.             NumToString(error, errNumStr);
  1020.             MAParamText("^ENUM", errNumStr);
  1021.             MAReplaceText(errStr);
  1022.         }
  1023.  
  1024.         WriteString(keyErrorString, errStr);
  1025.     }
  1026. }
  1027.  
  1028. //----------------------------------------------------------------------------------------
  1029. // TAppleEvent::ReadError:
  1030. //----------------------------------------------------------------------------------------
  1031. #pragma segment AEReadWrite
  1032.  
  1033. void TAppleEvent::ReadError(long& errorNumber,
  1034.                             CStr255& errorString)
  1035. {
  1036.     errorNumber = noErr;
  1037.  
  1038.     if (HasParameter(keyErrorNumber))
  1039.         errorNumber = (OSErr)ReadLong(keyErrorNumber);
  1040.  
  1041.     if (HasParameter(keyErrorString))
  1042.         ReadString(keyErrorString, errorString);
  1043. }
  1044.  
  1045. //========================================================================================
  1046. // CLASS CAEDesc
  1047. //========================================================================================
  1048. #undef Inherited
  1049.  
  1050. //----------------------------------------------------------------------------------------
  1051. // CAEDesc::CAEDesc: 
  1052. //----------------------------------------------------------------------------------------
  1053. #pragma segment ConstructorRes
  1054.  
  1055. CAEDesc::CAEDesc()
  1056. {
  1057.     fAEDesc.descriptorType = typeNull;
  1058.     fAEDesc.dataHandle = NULL;
  1059. }
  1060.  
  1061. //----------------------------------------------------------------------------------------
  1062. // CAEDesc::CAEDesc: 
  1063. //----------------------------------------------------------------------------------------
  1064. #pragma segment ConstructorRes
  1065.  
  1066. CAEDesc::CAEDesc(const AEDesc& theDesc) :
  1067.     fAEDesc(theDesc)
  1068. {
  1069. }
  1070.  
  1071. //----------------------------------------------------------------------------------------
  1072. // CAEDesc::CAEDesc: 
  1073. //----------------------------------------------------------------------------------------
  1074. #pragma segment ConstructorRes
  1075.  
  1076. CAEDesc::CAEDesc(const AEDesc* theDesc) :
  1077.     fAEDesc(*theDesc)
  1078. {
  1079. }
  1080.  
  1081. //----------------------------------------------------------------------------------------
  1082. // CAEDesc::CAEDesc: 
  1083. //----------------------------------------------------------------------------------------
  1084. #pragma segment ConstructorRes
  1085.  
  1086. CAEDesc::CAEDesc(DescType theType,
  1087.                  Handle theHandle)
  1088. {
  1089.     fAEDesc.dataHandle = NULL;
  1090.     SetDesc(theType, theHandle);
  1091. }
  1092.  
  1093. //--------------------------------------------------------------------------------------------------
  1094. // CAEDesc::CanCoerceDesc: 
  1095. //--------------------------------------------------------------------------------------------------
  1096. #pragma segment AEReadWrite
  1097.  
  1098. Boolean CAEDesc::CanCoerceDesc(DescType toType) const
  1099. {
  1100.     OSErr err;
  1101.     CTempDesc tempDesc;
  1102.     AEDesc tempDescriptor = fAEDesc;            // use temp to enable function to be declared
  1103.     // const
  1104.  
  1105.     err = AECoerceDesc(&tempDescriptor, toType, tempDesc);
  1106.  
  1107.     return (err == noErr);
  1108. }
  1109.  
  1110. //--------------------------------------------------------------------------------------------------
  1111. // CAEDesc::CoerceDesc: 
  1112. //--------------------------------------------------------------------------------------------------
  1113. #pragma segment AEReadWrite
  1114.  
  1115. void CAEDesc::CoerceDesc(DescType toType,
  1116.                          AEDesc* result) const
  1117. {
  1118.     AEDesc tempDescriptor = fAEDesc;            // use temp to enable function to be declared
  1119.     // const.
  1120.     FailOSErr(AECoerceDesc(&tempDescriptor, toType, result));
  1121. }
  1122.  
  1123. //--------------------------------------------------------------------------------------------------
  1124. // CAEDesc::CreateDesc: 
  1125. //--------------------------------------------------------------------------------------------------
  1126. #pragma segment AEReadWrite
  1127.  
  1128. void CAEDesc::CreateDesc(DescType typeCode,
  1129.                          const void* dataPtr,
  1130.                          Size dataSize)
  1131. {
  1132.     if (fAEDesc.dataHandle)                        // optimization
  1133.         FailOSErr(DisposeDesc());
  1134.  
  1135.     FailOSErr(AECreateDesc(typeCode, dataPtr, dataSize, &fAEDesc));
  1136. }
  1137.  
  1138. //--------------------------------------------------------------------------------------------------
  1139. // CAEDesc::DisposeDesc: 
  1140. //--------------------------------------------------------------------------------------------------
  1141. #pragma segment AEReadWrite
  1142.  
  1143. OSErr CAEDesc::DisposeDesc()
  1144. // DisposeDesc returns a status because it is used in destructors, where we can't fail,
  1145. // and it is also used independently, as in CreateDesc. 
  1146. {
  1147.     OSErr result = noErr;
  1148.  
  1149.     if (fAEDesc.dataHandle)
  1150.         result = AEDisposeDesc(&fAEDesc);
  1151.  
  1152.     if (result == noErr)
  1153.     {
  1154.         fAEDesc.dataHandle = NULL;
  1155.         fAEDesc.descriptorType = typeNull;
  1156.     }
  1157.     return result;
  1158. }
  1159.  
  1160. //--------------------------------------------------------------------------------------------------
  1161. // CAEDesc::GetKeyPtr:
  1162. //--------------------------------------------------------------------------------------------------
  1163. #pragma segment AEReadWrite
  1164.  
  1165. OSErr CAEDesc::GetKeyPtr(AEKeyword theAEKeyword,
  1166.                          DescType desiredType,
  1167.                          DescType* typeCode,
  1168.                          void* dataPtr,
  1169.                          Size maximumSize,
  1170.                          Size* actualSize) const
  1171. {
  1172.     AEDesc tempDescriptor = fAEDesc;            // use temp to enable function to be const.
  1173.  
  1174.     return AEGetKeyPtr(&tempDescriptor, theAEKeyword, desiredType, typeCode, dataPtr, maximumSize, actualSize);
  1175. }
  1176.  
  1177. //--------------------------------------------------------------------------------------------------
  1178. // CAEDesc::PutKeyPtr:
  1179. //--------------------------------------------------------------------------------------------------
  1180. #pragma segment AEReadWrite
  1181.  
  1182. void CAEDesc::PutKeyPtr(AEKeyword theAEKeyword,
  1183.                         DescType typeCode,
  1184.                         const void* dataPtr,
  1185.                         Size dataSize)
  1186. {
  1187.     FailOSErr(AEPutKeyPtr(&fAEDesc, theAEKeyword, typeCode, dataPtr, dataSize));
  1188. }
  1189.  
  1190. //--------------------------------------------------------------------------------------------------
  1191. // CAEDesc::SetDesc: 
  1192. //--------------------------------------------------------------------------------------------------
  1193. #pragma segment AEReadWrite
  1194.  
  1195. void CAEDesc::SetDesc(DescType theType,
  1196.                       Handle theHandle)
  1197. {
  1198.     fAEDesc.descriptorType = theType;
  1199.     fAEDesc.dataHandle = theHandle;
  1200. }
  1201.  
  1202. //--------------------------------------------------------------------------------------------------
  1203. // CAEDesc::EqualDesc: 
  1204. //--------------------------------------------------------------------------------------------------
  1205. #pragma segment AEReadWrite
  1206.  
  1207. Boolean CAEDesc::EqualDesc(const CAEDesc& theDesc) const
  1208. {
  1209.     return CompareDesc(theDesc, kAEEquals);
  1210. }
  1211.  
  1212. //--------------------------------------------------------------------------------------------------
  1213. // CAEDesc::CompareDesc: 
  1214. //--------------------------------------------------------------------------------------------------
  1215. #pragma segment AEReadWrite
  1216.  
  1217. Boolean CAEDesc::CompareDesc(const CAEDesc& theDesc,
  1218.                              DescType compareHow) const
  1219. {
  1220.     CAEDesc localDesc1 = *this;
  1221.     CAEDesc localDesc2 = theDesc;
  1222.     DescType type1 = localDesc1.GetDescriptorType();
  1223.     DescType type2 = localDesc2.GetDescriptorType();
  1224.     Boolean tossDesc1 = FALSE;
  1225.     Boolean tossDesc2 = FALSE;
  1226.     Boolean theResult = FALSE;
  1227.  
  1228.     if ((compareHow == kAEEquals) && (localDesc1.GetDataHandle() == localDesc2.GetDataHandle()))
  1229.         return TRUE;
  1230.  
  1231.     if (type1 != type2)
  1232.     {
  1233.         // Try coercing theDesc to type1
  1234.         if (AECoerceDesc(theDesc, type1, localDesc2) == noErr)
  1235.             tossDesc2 = TRUE;
  1236.         else
  1237.             // Try coercing myself to type2
  1238.             if (AECoerceDesc(*this, type2, localDesc1) == noErr)
  1239.                 tossDesc1 = TRUE;
  1240.             else
  1241.                 return FALSE;
  1242.     }
  1243.  
  1244.     type1 = localDesc1.GetDescriptorType();
  1245.     FailInfo fi;
  1246.     Try(fi)
  1247.     {
  1248.  
  1249.         switch (type1)
  1250.         {
  1251.             case typeAEList:
  1252.                 {
  1253.                     // Compare all the elements in the list
  1254.                     long items1 = localDesc1.CountListItems();
  1255.                     long items2 = localDesc2.CountListItems();
  1256.                     if (((compareHow == kAEEquals) && (items1 == items2)) || (((compareHow == kAEBeginsWith) || (compareHow == kAEEndsWith)) && (items1 >= items2)))
  1257.                     {
  1258.                         theResult = TRUE;        // empty lists are equal
  1259.                         long numToCheck = items1;
  1260.                         if ((compareHow == kAEBeginsWith) || (compareHow == kAEEndsWith))
  1261.                             numToCheck = items2;
  1262.                         long list1Indx = 1;
  1263.                         if (compareHow == kAEEndsWith)
  1264.                             list1Indx = (items1 - items2) + 1;
  1265.                         for (long i = 1; i <= numToCheck; i++)
  1266.                         {
  1267.                             CTempDesc list1Item;
  1268.                             CTempDesc list2Item;
  1269.                             localDesc1.GetNthItem(list1Indx++, typeWildCard, list1Item);
  1270.                             localDesc2.GetNthItem(i, typeWildCard, list2Item);
  1271.                             theResult &= list1Item.CompareDesc(list2Item, kAEEquals);
  1272.                         }
  1273.                     }
  1274.                     else
  1275.                         theResult = FALSE;
  1276.                     break;
  1277.                 }
  1278.  
  1279.             case typeChar:
  1280.                 {
  1281.                     Handle charsHdl1 = localDesc1.GetDataHandle();
  1282.                     Handle charsHdl2 = localDesc2.GetDataHandle();
  1283.                     SignedByte was1State = LockHandle(charsHdl1);
  1284.                     SignedByte was2State = LockHandle(charsHdl2);
  1285.                     short size1 = (short)localDesc1.GetDataSize();
  1286.                     short size2 = (short)localDesc2.GetDataSize();
  1287.                     short magIDResult = IUMagString(*charsHdl1, *charsHdl2, size1, size2);
  1288.                     switch (compareHow)
  1289.                     {
  1290.                         case kAEGreaterThan:
  1291.                             theResult = (magIDResult == 1);
  1292.                             break;
  1293.                         case kAEGreaterThanEquals:
  1294.                             theResult = (magIDResult != -1);
  1295.                             break;
  1296.                         case kAEEquals:
  1297.                             theResult = (magIDResult == 0);
  1298.                             break;
  1299.                         case kAELessThan:
  1300.                             theResult = (magIDResult == -1);
  1301.                             break;
  1302.                         case kAELessThanEquals:
  1303.                             theResult = (magIDResult != 1);
  1304.                             break;
  1305.                         case kAEBeginsWith:
  1306.                             {
  1307.                                 if (size1 >= size2)
  1308.                                     theResult = (IUMagString(*charsHdl1, *charsHdl2, size2, size2) == 0);
  1309.                                 break;
  1310.                             }
  1311.                         case kAEEndsWith:
  1312.                             {
  1313.                                 long theOffset = size1 - size2;
  1314.                                 if (theOffset >= 0)
  1315.                                     theResult = (IUMagString(*charsHdl1 + theOffset, *charsHdl2, size2, size2) == 0);
  1316.                                 break;
  1317.                             }
  1318.                         case kAEContains:
  1319.                             {
  1320.                                 if (size1 >= size2)
  1321.                                     theResult = (Munger(charsHdl1, 0, *charsHdl2, size2, NULL, 0) >= 0);
  1322.                                 break;
  1323.                             }
  1324.                     }
  1325.                     HSetState(charsHdl1, was1State);
  1326.                     HSetState(charsHdl2, was2State);
  1327.  
  1328.                     break;
  1329.                 }
  1330.  
  1331.                 // Compare rectangles
  1332.             case typeQDRectangle:
  1333.                 {
  1334.                     CRect rect1;
  1335.                     localDesc1.GetRect(rect1);
  1336.                     CRect rect2;
  1337.                     localDesc2.GetRect(rect2);
  1338.                     if (compareHow == kAEEquals)
  1339.                         theResult = rect1 == rect2;
  1340.                     else if (compareHow == kAEContains)
  1341.                         theResult = rect1.Contains(rect2);
  1342.                     break;
  1343.                 }
  1344.  
  1345.                 // Compare colors
  1346.             case typeRGBColor:
  1347.                 if (compareHow == kAEEquals)
  1348.                 {
  1349.                     CRGBColor myPropColor;
  1350.                     localDesc1.GetRGBColor(myPropColor);
  1351.                     CRGBColor compareColor;
  1352.                     localDesc2.GetRGBColor(compareColor);
  1353.                     theResult = myPropColor == compareColor;
  1354.                     break;
  1355.                 }
  1356.  
  1357.             case typeLongDateTime:
  1358.                 {
  1359.                     unsigned long num1 = 0;
  1360.                     unsigned long num2 = 0;
  1361.                     num1 = (unsigned long)localDesc1.GetDateTime();
  1362.                     num2 = (unsigned long)localDesc2.GetDateTime();
  1363.                     switch (compareHow)
  1364.                     {
  1365.                         case kAEGreaterThan:
  1366.                             theResult = num1 > num2;
  1367.                             break;
  1368.                         case kAEGreaterThanEquals:
  1369.                             theResult = num1 >= num2;
  1370.                             break;
  1371.                         case kAEEquals:
  1372.                             theResult = num1 == num2;
  1373.                             break;
  1374.                         case kAELessThan:
  1375.                             theResult = num1 < num2;
  1376.                             break;
  1377.                         case kAELessThanEquals:
  1378.                             theResult = num1 <= num2;
  1379.                             break;
  1380.                     }
  1381.                 }
  1382.                 break;
  1383.  
  1384.                 // Handle all the types we can convert to longs and then compare
  1385.             case typeType:
  1386.             case typeEnumerated:
  1387.             case typeBoolean:
  1388.             case typeLongInteger:
  1389.             case typeShortInteger:
  1390.                 {
  1391.                     long num1 = 0;
  1392.                     long num2 = 0;
  1393.  
  1394.                     switch (type1)
  1395.                     {
  1396.                             // There are no coercion handlers for some of these types so we'll get them by
  1397.                             // the descriptor type then cast the results to longs.
  1398.                         case typeEnumerated:
  1399.                             {
  1400.                                 num1 = (long)localDesc1.GetEnum();
  1401.                                 num2 = (long)localDesc2.GetEnum();
  1402.                                 break;
  1403.                             }
  1404.                         case typeType:
  1405.                             {
  1406.                                 num1 = (long)localDesc1.GetType();
  1407.                                 num2 = (long)localDesc2.GetType();
  1408.                                 break;
  1409.                             }
  1410.                         case typeBoolean:
  1411.                             {
  1412.                                 num1 = (long)localDesc1.GetBoolean();
  1413.                                 num2 = (long)localDesc2.GetBoolean();
  1414.                                 break;
  1415.                             }
  1416.                         default:
  1417.                             {
  1418.                                 num1 = localDesc1.GetLong();
  1419.                                 num2 = localDesc2.GetLong();
  1420.                             }
  1421.                     }
  1422.                     switch (compareHow)
  1423.                     {
  1424.                         case kAEGreaterThan:
  1425.                             theResult = num1 > num2;
  1426.                             break;
  1427.                         case kAEGreaterThanEquals:
  1428.                             theResult = num1 >= num2;
  1429.                             break;
  1430.                         case kAEEquals:
  1431.                             theResult = num1 == num2;
  1432.                             break;
  1433.                         case kAELessThan:
  1434.                             theResult = num1 < num2;
  1435.                             break;
  1436.                         case kAELessThanEquals:
  1437.                             theResult = num1 <= num2;
  1438.                             break;
  1439.                     }
  1440.                     break;
  1441.                 }
  1442.  
  1443.             default:
  1444.                 {
  1445.                     // Check for the same handle content
  1446.                     long size1 = localDesc1.GetDataSize();
  1447.                     long size2 = localDesc2.GetDataSize();
  1448.                     Handle data1 = localDesc1.GetDataHandle();
  1449.                     Handle data2 = localDesc2.GetDataHandle();
  1450.                     theResult = ((compareHow == kAEEquals) && (size1 == size2) && (EqualBlocks(*data1, *data2, (short)size1)));
  1451.                 }
  1452.         }
  1453.         fi.Success();
  1454.     }
  1455.     else
  1456.     {
  1457.         // Don't resignal failure, just return false
  1458.         theResult = FALSE;
  1459.     }
  1460.     if (tossDesc1)
  1461.         localDesc1.DisposeDesc();
  1462.     if (tossDesc2)
  1463.         localDesc2.DisposeDesc();
  1464.     return theResult;
  1465. }
  1466.  
  1467. //--------------------------------------------------------------------------------------------------
  1468. // CAEDesc::GetDataSize: 
  1469. //--------------------------------------------------------------------------------------------------
  1470. #pragma segment AEReadWrite
  1471.  
  1472. long CAEDesc::GetDataSize() const
  1473. {
  1474.     return fAEDesc.dataHandle ? ::GetHandleSize(fAEDesc.dataHandle) : 0;
  1475. }
  1476.  
  1477. //--------------------------------------------------------------------------------------------------
  1478. // CAEDesc::CreateList: 
  1479. //--------------------------------------------------------------------------------------------------
  1480. #pragma segment AEReadWrite
  1481.  
  1482. void CAEDesc::CreateList()
  1483. {
  1484.     FailOSErr(AECreateList(NULL, 0, FALSE, &fAEDesc));
  1485. }
  1486.  
  1487. //--------------------------------------------------------------------------------------------------
  1488. // CAEDesc::CountListItems: 
  1489. //--------------------------------------------------------------------------------------------------
  1490. #pragma segment AEReadWrite
  1491.  
  1492. long CAEDesc::CountListItems()
  1493. {
  1494.     long theCount = 0;
  1495.     FailOSErr(AECountItems(&fAEDesc, &theCount));
  1496.  
  1497.     return theCount;
  1498. }
  1499.  
  1500. //--------------------------------------------------------------------------------------------------
  1501. // CAEDesc::GetNthItem: 
  1502. //--------------------------------------------------------------------------------------------------
  1503. #pragma segment AEReadWrite
  1504.  
  1505. void CAEDesc::GetNthItem(long index,
  1506.                          DescType desiredType,
  1507.                          CAEDesc& theDesc)
  1508. {
  1509.     DescType theAEKeyword;
  1510.     FailOSErr(AEGetNthDesc(&fAEDesc, index, desiredType, &theAEKeyword, theDesc));
  1511. }
  1512.  
  1513. //--------------------------------------------------------------------------------------------------
  1514. // CAEDesc::PutListItem: 
  1515. //--------------------------------------------------------------------------------------------------
  1516. #pragma segment AEReadWrite
  1517.  
  1518. void CAEDesc::PutListItem(long index,
  1519.                           CAEDesc& theDesc)
  1520. {
  1521.     FailOSErr(AEPutDesc(&fAEDesc, index, theDesc));
  1522. }
  1523.  
  1524. //--------------------------------------------------------------------------------------------------
  1525. // CAEDesc::DeleteListItem: 
  1526. //--------------------------------------------------------------------------------------------------
  1527. #pragma segment AEReadWrite
  1528.  
  1529. OSErr CAEDesc::DeleteListItem(long index)
  1530. {
  1531.     return AEDeleteItem(&fAEDesc, index);
  1532. }
  1533.  
  1534. //--------------------------------------------------------------------------------------------------
  1535. // CAEDesc::CreateRecord: 
  1536. //--------------------------------------------------------------------------------------------------
  1537. #pragma segment AEReadWrite
  1538.  
  1539. void CAEDesc::CreateRecord()
  1540. {
  1541.     FailOSErr(AECreateList(NULL, 0, TRUE, &fAEDesc));
  1542. }
  1543.  
  1544. //--------------------------------------------------------------------------------------------------
  1545. // CAEDesc::GetFSSpec: Note that this method provides transparent coercion of alias records into
  1546. // FSSpec records.
  1547. //--------------------------------------------------------------------------------------------------
  1548. #pragma segment AEReadWrite
  1549.  
  1550. void CAEDesc::GetFSSpec(FSSpec& fileSpec) const
  1551. {
  1552.     if (fAEDesc.descriptorType == typeFSS)
  1553.         fileSpec = **(FSSpec * *)fAEDesc.dataHandle;
  1554.     else
  1555.     {
  1556.         CTempDesc whichPropertyDesc;
  1557.         CoerceDesc(typeFSS, whichPropertyDesc);
  1558.         fileSpec = **(FSSpec * *)whichPropertyDesc.fAEDesc.dataHandle;
  1559.     }
  1560. }
  1561.  
  1562. //--------------------------------------------------------------------------------------------------
  1563. // CAEDesc::PutFSSpec: 
  1564. //--------------------------------------------------------------------------------------------------
  1565. #pragma segment AEReadWrite
  1566.  
  1567. void CAEDesc::PutFSSpec(const FSSpec& fileSpec)
  1568. {
  1569.     CreateDesc(typeFSS, &fileSpec, sizeof(fileSpec));
  1570. }
  1571.  
  1572. //--------------------------------------------------------------------------------------------------
  1573. // CAEDesc::GetRect: 
  1574. //--------------------------------------------------------------------------------------------------
  1575. #pragma segment AEReadWrite
  1576.  
  1577. void CAEDesc::GetRect(CRect& theRect) const
  1578. {
  1579.     if (fAEDesc.descriptorType == typeQDRectangle)
  1580.         theRect = **(CRect * *)fAEDesc.dataHandle;
  1581.     else
  1582.     {
  1583.         CTempDesc whichPropertyDesc;
  1584.         CoerceDesc(typeQDRectangle, whichPropertyDesc);
  1585.         theRect = **(CRect * *)whichPropertyDesc.fAEDesc.dataHandle;
  1586.     }
  1587. }
  1588.  
  1589. //--------------------------------------------------------------------------------------------------
  1590. // CAEDesc::PutRect: 
  1591. //--------------------------------------------------------------------------------------------------
  1592. #pragma segment AEReadWrite
  1593.  
  1594. void CAEDesc::PutRect(const CRect& theRect)
  1595. {
  1596.     CreateDesc(typeQDRectangle, &theRect, sizeof(theRect));
  1597. }
  1598.  
  1599. //--------------------------------------------------------------------------------------------------
  1600. // CAEDesc::GetType: 
  1601. //--------------------------------------------------------------------------------------------------
  1602. #pragma segment AEReadWrite
  1603.  
  1604. DescType CAEDesc::GetType() const
  1605. {
  1606.     DescType result;
  1607.     OSErr err = 0;
  1608.     if (fAEDesc.descriptorType == typeType)
  1609.         result = **(DescType * *)fAEDesc.dataHandle;
  1610.     else
  1611.     {
  1612.         CTempDesc whichPropertyDesc;
  1613.         CoerceDesc(typeType, whichPropertyDesc);
  1614.         result = **(DescType * *)whichPropertyDesc.fAEDesc.dataHandle;
  1615.     }
  1616.  
  1617.     return result;
  1618. }
  1619.  
  1620. //--------------------------------------------------------------------------------------------------
  1621. // CAEDesc::PutType: 
  1622. //--------------------------------------------------------------------------------------------------
  1623. #pragma segment AEReadWrite
  1624.  
  1625. void CAEDesc::PutType(DescType theType)
  1626. {
  1627.     CreateDesc(typeType, &theType, sizeof(theType));
  1628. }
  1629.  
  1630. //--------------------------------------------------------------------------------------------------
  1631. // CAEDesc::GetString: 
  1632. //--------------------------------------------------------------------------------------------------
  1633. #pragma segment AEReadWrite
  1634.  
  1635. void CAEDesc::GetString(CStr255& theString,
  1636.                         long maximumSize) const
  1637. {
  1638.     long theSize = Min(GetDataSize(), maximumSize);
  1639.     if (theSize)
  1640.     {
  1641.         if (fAEDesc.descriptorType == typeChar)
  1642.             theString.CopyFrom((void*) * fAEDesc.dataHandle, theSize);
  1643.         else
  1644.         {
  1645.             CTempDesc myTempDesc;
  1646.             CoerceDesc(typeChar, myTempDesc);
  1647.  
  1648.             theString.CopyFrom((void*) * myTempDesc.fAEDesc.dataHandle, Min(maximumSize, myTempDesc.GetDataSize()));
  1649.         }
  1650.     }
  1651. }
  1652.  
  1653. //--------------------------------------------------------------------------------------------------
  1654. // CAEDesc::PutString: 
  1655. //--------------------------------------------------------------------------------------------------
  1656. #pragma segment AEReadWrite
  1657.  
  1658. void CAEDesc::PutString(const CStr255& theString)
  1659. {
  1660.     CreateDesc(typeChar, &theString.fStr[1], theString.Length());
  1661. }
  1662.  
  1663. //--------------------------------------------------------------------------------------------------
  1664. // CAEDesc::GetBoolean: 
  1665. //--------------------------------------------------------------------------------------------------
  1666. #pragma segment AEReadWrite
  1667.  
  1668. Boolean CAEDesc::GetBoolean() const
  1669. {
  1670.     Boolean result;
  1671.     if (fAEDesc.descriptorType == typeBoolean)
  1672.         result = **(Boolean * *)fAEDesc.dataHandle;
  1673.     else
  1674.     {
  1675.         CTempDesc myTempDesc;
  1676.         CoerceDesc(typeBoolean, myTempDesc);
  1677.         result = **(Boolean * *)myTempDesc.fAEDesc.dataHandle;
  1678.     }
  1679.  
  1680.     return result;
  1681. }
  1682.  
  1683. //--------------------------------------------------------------------------------------------------
  1684. // CAEDesc::PutBoolean: 
  1685. //--------------------------------------------------------------------------------------------------
  1686. #pragma segment AEReadWrite
  1687.  
  1688. void CAEDesc::PutBoolean(Boolean theBoolean)
  1689. {
  1690.     CreateDesc(typeBoolean, &theBoolean, sizeof(theBoolean));
  1691. }
  1692.  
  1693. //--------------------------------------------------------------------------------------------------
  1694. // CAEDesc::GetEnum: 
  1695. //--------------------------------------------------------------------------------------------------
  1696. #pragma segment AEReadWrite
  1697.  
  1698. DescType CAEDesc::GetEnum() const
  1699. {
  1700.     DescType result;
  1701.     if (fAEDesc.descriptorType == typeEnumeration)
  1702.         result = **(DescType * *)fAEDesc.dataHandle;
  1703.     else
  1704.     {
  1705.         CTempDesc myTempDesc;
  1706.         CoerceDesc(typeEnumeration, myTempDesc);
  1707.         result = **(DescType * *)myTempDesc.fAEDesc.dataHandle;
  1708.     }
  1709.  
  1710.     return result;
  1711. }
  1712.  
  1713. //--------------------------------------------------------------------------------------------------
  1714. // CAEDesc::PutEnum:
  1715. //--------------------------------------------------------------------------------------------------
  1716. #pragma segment AEReadWrite
  1717.  
  1718. void CAEDesc::PutEnum(DescType theEnum)
  1719. {
  1720.     CreateDesc(typeEnumeration, &theEnum, sizeof(theEnum));
  1721. }
  1722.  
  1723. //--------------------------------------------------------------------------------------------------
  1724. // CAEDesc::GetLong:
  1725. //--------------------------------------------------------------------------------------------------
  1726. #pragma segment AEReadWrite
  1727.  
  1728. long CAEDesc::GetLong() const
  1729. {
  1730.     long result;
  1731.     if (fAEDesc.descriptorType == typeLongInteger)
  1732.         result = **(long**)fAEDesc.dataHandle;
  1733.     else
  1734.     {
  1735.         CTempDesc myTempDesc;
  1736.         CoerceDesc(typeLongInteger, myTempDesc);
  1737.         result = **(long**)myTempDesc.fAEDesc.dataHandle;
  1738.     }
  1739.  
  1740.     return result;
  1741. }
  1742.  
  1743. //--------------------------------------------------------------------------------------------------
  1744. // CAEDesc::PutLong:
  1745. //--------------------------------------------------------------------------------------------------
  1746. #pragma segment AEReadWrite
  1747.  
  1748. void CAEDesc::PutLong(long theLong)
  1749. {
  1750.     CreateDesc(typeLongInteger, &theLong, sizeof(theLong));
  1751. }
  1752.  
  1753. //--------------------------------------------------------------------------------------------------
  1754. // CAEDesc::GetDateTime:
  1755. //--------------------------------------------------------------------------------------------------
  1756. #pragma segment AEReadWrite
  1757.  
  1758. unsigned long CAEDesc::GetDateTime() const
  1759. {
  1760.     LongDateTime date;
  1761.     ((LongDateCvt *) & date)->hl.lHigh = 0;
  1762.     ((LongDateCvt *) & date)->hl.lLow = 0;
  1763.  
  1764.     if (fAEDesc.descriptorType == typeLongDateTime)
  1765.         date = **(LongDateTime * *)fAEDesc.dataHandle;
  1766.     else
  1767.     {
  1768.         CTempDesc myTempDesc;
  1769.         CoerceDesc(typeLongDateTime, myTempDesc);
  1770.         date = **(LongDateTime * *)myTempDesc.fAEDesc.dataHandle;
  1771.     }
  1772.  
  1773.     return ((LongDateCvt *) & date)->hl.lLow;
  1774. }
  1775.  
  1776. //--------------------------------------------------------------------------------------------------
  1777. // CAEDesc::PutDateTime:
  1778. //--------------------------------------------------------------------------------------------------
  1779. #pragma segment AEReadWrite
  1780.  
  1781. void CAEDesc::PutDateTime(unsigned long theDateTime)
  1782. {
  1783.     LongDateTime date;
  1784.     ((LongDateCvt *) & date)->hl.lHigh = 0;
  1785.     ((LongDateCvt *) & date)->hl.lLow = theDateTime;
  1786.     CreateDesc(typeLongDateTime, &date, sizeof(date));
  1787. }
  1788.  
  1789. //--------------------------------------------------------------------------------------------------
  1790. // CAEDesc::GetRGBColor:
  1791. //--------------------------------------------------------------------------------------------------
  1792. #pragma segment AEReadWrite
  1793.  
  1794. void CAEDesc::GetRGBColor(CRGBColor& theColor) const
  1795. {
  1796.     if (fAEDesc.descriptorType == typeRGBColor)
  1797.         theColor = **(CRGBColor * *)fAEDesc.dataHandle;
  1798.     else
  1799.     {
  1800.         CTempDesc myTempDesc;
  1801.         CoerceDesc(typeRGBColor, myTempDesc);
  1802.         theColor = **(CRGBColor * *)myTempDesc.fAEDesc.dataHandle;
  1803.     }
  1804. }
  1805.  
  1806. //--------------------------------------------------------------------------------------------------
  1807. // CAEDesc::PutRGBColor:
  1808. //--------------------------------------------------------------------------------------------------
  1809. #pragma segment AEReadWrite
  1810.  
  1811. void CAEDesc::PutRGBColor(const CRGBColor& theColor)
  1812. {
  1813.     CreateDesc(typeRGBColor, &theColor, sizeof(theColor));
  1814. }
  1815.  
  1816. //--------------------------------------------------------------------------------------------------
  1817. // CAEDesc::GetObject:
  1818. //--------------------------------------------------------------------------------------------------
  1819. #pragma segment AEReadWrite
  1820.  
  1821. MScriptableObject* CAEDesc::GetObject() const
  1822. {
  1823.     MScriptableObject * theResult;
  1824.     if (fAEDesc.dataHandle)
  1825.         theResult = **(MScriptableObject * **)fAEDesc.dataHandle;
  1826.     else
  1827.         theResult = gDispatcher;
  1828.  
  1829.     return theResult;
  1830. }
  1831.  
  1832. //--------------------------------------------------------------------------------------------------
  1833. // CAEDesc::PutObject:
  1834. //--------------------------------------------------------------------------------------------------
  1835. #pragma segment AEReadWrite
  1836.  
  1837. void CAEDesc::PutObject(MScriptableObject* theObject)
  1838. {
  1839.     CreateDesc(theObject->GetOMClass(), &theObject, sizeof(theObject));
  1840. }
  1841.  
  1842. //--------------------------------------------------------------------------------------------------
  1843. // CAEDesc::GetKeyString:
  1844. //--------------------------------------------------------------------------------------------------
  1845. #pragma segment AEReadWrite
  1846.  
  1847. Boolean CAEDesc::GetKeyString(DescType theKey,
  1848.                               CStr255& theStr,
  1849.                               long maximumSize) const
  1850. {
  1851.     CStr255 localString;
  1852.     DescType actualType;
  1853.     long actualSize = 0;
  1854.     OSErr iErr = GetKeyPtr(theKey, typeChar, &actualType, &localString[1], maximumSize, &actualSize);
  1855.     if (iErr == 0)
  1856.     {
  1857.         localString[0] = (unsigned char)actualSize;
  1858.         theStr = localString;
  1859.     }
  1860.  
  1861.     return (iErr == 0);
  1862. }
  1863.  
  1864. //--------------------------------------------------------------------------------------------------
  1865. // CAEDesc::GetKeyEnum:
  1866. //--------------------------------------------------------------------------------------------------
  1867. #pragma segment AEReadWrite
  1868.  
  1869. Boolean CAEDesc::GetKeyEnum(DescType theKey,
  1870.                             DescType& theEnum) const
  1871. {
  1872.     DescType localEnum;
  1873.     DescType actualType;
  1874.     long actualSize = 0;
  1875.     OSErr iErr = GetKeyPtr(theKey, typeEnumeration, &actualType, &localEnum, 4, &actualSize);
  1876.     if (iErr == 0)
  1877.         theEnum = localEnum;
  1878.  
  1879.     return (iErr == 0);
  1880. }
  1881.  
  1882. //--------------------------------------------------------------------------------------------------
  1883. // CAEDesc::GetKeyDesc:
  1884. //--------------------------------------------------------------------------------------------------
  1885. #pragma segment AEReadWrite
  1886.  
  1887. Boolean CAEDesc::GetKeyDesc(DescType theKey,
  1888.                             DescType desiredType,
  1889.                             CAEDesc& theAEDesc) const
  1890. {
  1891.     CAEDesc localDesc;
  1892.  
  1893.     AEDesc tempDescriptor = fAEDesc;            // temp to allow function to be const.
  1894.  
  1895.     OSErr iErr = AEGetKeyDesc(&tempDescriptor, theKey, desiredType, localDesc);
  1896.     if (iErr == 0)
  1897.         theAEDesc = localDesc;
  1898.  
  1899.     return (iErr == 0);
  1900. }
  1901.  
  1902. //--------------------------------------------------------------------------------------------------
  1903. // CAEDesc::GetKeyLong:
  1904. //--------------------------------------------------------------------------------------------------
  1905. #pragma segment AEReadWrite
  1906.  
  1907. Boolean CAEDesc::GetKeyLong(DescType theKey,
  1908.                             long& theNum) const
  1909. {
  1910.     long localNum;
  1911.     DescType actualType;
  1912.     long actualSize = 0;
  1913.     OSErr iErr = GetKeyPtr(theKey, typeLongInteger, &actualType, &localNum, 4, &actualSize);
  1914.     if (iErr == 0)
  1915.         theNum = localNum;
  1916.  
  1917.     return (iErr == 0);
  1918. }
  1919.  
  1920. //--------------------------------------------------------------------------------------------------
  1921. // CAEDesc::GetKeyRect:
  1922. //--------------------------------------------------------------------------------------------------
  1923. #pragma segment AEReadWrite
  1924.  
  1925. Boolean CAEDesc::GetKeyRect(DescType theKey,
  1926.                             CRect& theRect) const
  1927. {
  1928.     CRect localRect;
  1929.     DescType actualType;
  1930.     long actualSize = 0;
  1931.     OSErr iErr = GetKeyPtr(theKey, typeQDRectangle, &actualType, &localRect, sizeof(localRect), &actualSize);
  1932.     if (iErr == 0)
  1933.         theRect = localRect;
  1934.  
  1935.     return (iErr == 0);
  1936. }
  1937.  
  1938. //--------------------------------------------------------------------------------------------------
  1939. // CAEDesc::GetKeyDateTime:
  1940. //--------------------------------------------------------------------------------------------------
  1941. #pragma segment AEReadWrite
  1942.  
  1943. Boolean CAEDesc::GetKeyDateTime(DescType theKey,
  1944.                                 unsigned long& theDate) const
  1945. {
  1946.     LongDateTime date;
  1947.     ((LongDateCvt *) & date)->hl.lHigh = 0;
  1948.     ((LongDateCvt *) & date)->hl.lLow = 0;
  1949.     DescType actualType;
  1950.     long actualSize = 0;
  1951.     OSErr iErr = GetKeyPtr(theKey, typeLongDateTime, &actualType, &date, sizeof(date), &actualSize);
  1952.     if (iErr == noErr)
  1953.         theDate = ((LongDateCvt *) & date)->hl.lLow;
  1954.  
  1955.     return (iErr == 0);
  1956. }
  1957.  
  1958. //--------------------------------------------------------------------------------------------------
  1959. // CAEDesc::GetKeyBoolean:
  1960. //--------------------------------------------------------------------------------------------------
  1961. #pragma segment AEReadWrite
  1962.  
  1963. Boolean CAEDesc::GetKeyBoolean(DescType theKey,
  1964.                                Boolean& theBoolean) const
  1965. {
  1966.     Boolean localBoolean;
  1967.     DescType actualType;
  1968.     long actualSize = 0;
  1969.     OSErr iErr = GetKeyPtr(theKey, typeBoolean, &actualType, &localBoolean, sizeof(localBoolean), &actualSize);
  1970.     if (iErr == noErr)
  1971.         theBoolean = localBoolean;
  1972.  
  1973.     return (iErr == 0);
  1974. }
  1975.  
  1976. //--------------------------------------------------------------------------------------------------
  1977. // CAEDesc::GetKeyColor:
  1978. //--------------------------------------------------------------------------------------------------
  1979. #pragma segment AEReadWrite
  1980.  
  1981. Boolean CAEDesc::GetKeyColor(DescType theKey,
  1982.                              CRGBColor& theColor) const
  1983. {
  1984.     CRGBColor localColor;
  1985.     DescType actualType;
  1986.     long actualSize = 0;
  1987.     OSErr iErr = GetKeyPtr(theKey, typeRGBColor, &actualType, &localColor, sizeof(localColor), &actualSize);
  1988.     if (iErr == 0)
  1989.         theColor = localColor;
  1990.  
  1991.     return (iErr == 0);
  1992. }
  1993.  
  1994. //--------------------------------------------------------------------------------------------------
  1995. // CAEDesc::PutKeyString:
  1996. //--------------------------------------------------------------------------------------------------
  1997. #pragma segment AEReadWrite
  1998.  
  1999. void CAEDesc::PutKeyString(DescType theKey,
  2000.                            const CStr255& theStr)
  2001. {
  2002.     CStr255 localString = theStr;
  2003.     PutKeyPtr(theKey, typeChar, &localString[1], localString.Length());
  2004. }
  2005.  
  2006. //--------------------------------------------------------------------------------------------------
  2007. // CAEDesc::PutKeyEnum:
  2008. //--------------------------------------------------------------------------------------------------
  2009. #pragma segment AEReadWrite
  2010.  
  2011. void CAEDesc::PutKeyEnum(DescType theKey,
  2012.                          DescType theEnum)
  2013. {
  2014.     PutKeyPtr(theKey, typeEnumeration, &theEnum, 4);
  2015. }
  2016.  
  2017. //--------------------------------------------------------------------------------------------------
  2018. // CAEDesc::PutKeyDateTime:
  2019. //--------------------------------------------------------------------------------------------------
  2020. #pragma segment AEReadWrite
  2021.  
  2022. void CAEDesc::PutKeyDateTime(DescType theKey,
  2023.                              unsigned long theDate)
  2024. {
  2025.     LongDateTime date;
  2026.     ((LongDateCvt *) & date)->hl.lHigh = 0;
  2027.     ((LongDateCvt *) & date)->hl.lLow = theDate;
  2028.     PutKeyPtr(theKey, typeLongDateTime, &date, sizeof(date));
  2029. }
  2030.  
  2031. //--------------------------------------------------------------------------------------------------
  2032. // CAEDesc::DeleteKeyDesc:
  2033. //--------------------------------------------------------------------------------------------------
  2034. #pragma segment AEReadWrite
  2035.  
  2036. OSErr CAEDesc::DeleteKeyDesc(DescType theKey)
  2037. {
  2038.     return AEDeleteKeyDesc(&fAEDesc, theKey);
  2039. }
  2040.  
  2041. //--------------------------------------------------------------------------------------------------
  2042. // CAEDesc::PutKeyDesc:
  2043. //--------------------------------------------------------------------------------------------------
  2044. #pragma segment AEReadWrite
  2045.  
  2046. void CAEDesc::PutKeyDesc(DescType theKey,
  2047.                          CAEDesc& theAEDesc)
  2048. {
  2049.     FailOSErr(AEPutKeyDesc(&fAEDesc, theKey, theAEDesc));
  2050. }
  2051.  
  2052. //--------------------------------------------------------------------------------------------------
  2053. // CAEDesc::PutKeyLong:
  2054. //--------------------------------------------------------------------------------------------------
  2055. #pragma segment AEReadWrite
  2056.  
  2057. void CAEDesc::PutKeyLong(DescType theKey,
  2058.                          long theNum)
  2059. {
  2060.     PutKeyPtr(theKey, typeLongInteger, &theNum, sizeof(theNum));
  2061. }
  2062.  
  2063. //--------------------------------------------------------------------------------------------------
  2064. // CAEDesc::PutKeyRect:
  2065. //--------------------------------------------------------------------------------------------------
  2066. #pragma segment AEReadWrite
  2067.  
  2068. void CAEDesc::PutKeyRect(DescType theKey,
  2069.                          const CRect& theRect)
  2070. {
  2071.     PutKeyPtr(theKey, typeQDRectangle, &theRect, sizeof(theRect));
  2072. }
  2073.  
  2074. //--------------------------------------------------------------------------------------------------
  2075. // CAEDesc::PutKeyBoolean:
  2076. //--------------------------------------------------------------------------------------------------
  2077. #pragma segment AEReadWrite
  2078.  
  2079. void CAEDesc::PutKeyBoolean(DescType theKey,
  2080.                             Boolean theBoolean)
  2081. {
  2082.     PutKeyPtr(theKey, typeBoolean, &theBoolean, sizeof(theBoolean));
  2083. }
  2084.  
  2085. //--------------------------------------------------------------------------------------------------
  2086. // CAEDesc::PutKeyColor:
  2087. //--------------------------------------------------------------------------------------------------
  2088. #pragma segment AEReadWrite
  2089.  
  2090. void CAEDesc::PutKeyColor(DescType theKey,
  2091.                           const CRGBColor& theColor)
  2092. {
  2093.     PutKeyPtr(theKey, typeRGBColor, &theColor, sizeof(theColor));
  2094. }
  2095.  
  2096. //--------------------------------------------------------------------------------------------------
  2097. // CAEDesc::operator=:
  2098. //--------------------------------------------------------------------------------------------------
  2099. #pragma segment AEReadWrite
  2100.  
  2101. CAEDesc& CAEDesc::operator=(const CAEDesc& theDesc)
  2102. {
  2103.     fAEDesc = theDesc.fAEDesc;
  2104.     return *this;
  2105. }
  2106.  
  2107. //--------------------------------------------------------------------------------------------------
  2108. // CAEDesc::ReadFrom
  2109. //--------------------------------------------------------------------------------------------------
  2110. #pragma segment AEReadWrite
  2111.  
  2112. void CAEDesc::ReadFrom(TStream* aStream)
  2113. {
  2114.     FailOSErr(DisposeDesc());
  2115.     fAEDesc.descriptorType = (DescType)aStream->ReadLong();
  2116.     Boolean hasHandle = aStream->ReadBoolean();
  2117.     if (hasHandle)
  2118.         fAEDesc.dataHandle = aStream->ReadHandle();
  2119. }
  2120.  
  2121. //--------------------------------------------------------------------------------------------------
  2122. // CAEDesc::ReadFrom
  2123. //--------------------------------------------------------------------------------------------------
  2124. #pragma segment AEReadWrite
  2125.  
  2126. void CAEDesc::ReadFrom(TFile* aFile,
  2127.                        ResType resourceType,
  2128.                        short resourceID)
  2129. {
  2130. #if qDebug
  2131.     FailNonObject(aFile);
  2132. #endif
  2133.  
  2134.     FailOSErr(DisposeDesc());
  2135.     if (aFile->HasRsrcFork())
  2136.     {
  2137.         Handle dataHandle;
  2138.  
  2139.         short savedRefNum = aFile->UseResource();
  2140.         dataHandle = Get1Resource(resourceType, resourceID);
  2141.         MAUseResFile(savedRefNum);
  2142.  
  2143.         if (dataHandle)
  2144.         {
  2145.             fAEDesc.descriptorType = resourceType;
  2146.             DetachResource(dataHandle);
  2147.             fAEDesc.dataHandle = dataHandle;
  2148.         }
  2149.     }
  2150. }
  2151.  
  2152. //--------------------------------------------------------------------------------------------------
  2153. // CAEDesc::WriteTo
  2154. //--------------------------------------------------------------------------------------------------
  2155. #pragma segment AEReadWrite
  2156.  
  2157. void CAEDesc::WriteTo(TStream* aStream)
  2158. {
  2159.     aStream->WriteLong((long)GetDescriptorType());
  2160.     Handle theDataHandle = GetDataHandle();
  2161.     aStream->WriteBoolean(theDataHandle != NULL);
  2162.     if (theDataHandle)
  2163.         aStream->WriteHandle(theDataHandle);
  2164. }
  2165.  
  2166. //--------------------------------------------------------------------------------------------------
  2167. // CAEDesc::WriteTo
  2168. //--------------------------------------------------------------------------------------------------
  2169. #pragma segment AEReadWrite
  2170.  
  2171. void CAEDesc::WriteTo(TFile* aFile,
  2172.                       short resourceID,
  2173.                       CStr255& resourceName)
  2174. {
  2175. #if qDebug
  2176.     FailNonObject(aFile);
  2177. #endif
  2178.  
  2179.     if (aFile->HasRsrcFork() && fAEDesc.dataHandle != NULL)
  2180.     {
  2181.         Handle dataHandle = fAEDesc.dataHandle;    // not volatile...passed by address
  2182.  
  2183.         FailInfo fi;
  2184.         Try(fi)
  2185.         {
  2186.             FailOSErr(HandToHand(&dataHandle));
  2187.  
  2188.             short savedResFile = aFile->UseResource();
  2189.             MAAddResource(dataHandle, fAEDesc.descriptorType, resourceID, resourceName);
  2190.             FailResError();
  2191.             WriteResource(dataHandle);
  2192.             ReleaseResource(dataHandle);
  2193.  
  2194.             MAUseResFile(savedResFile);
  2195.             fi.Success();
  2196.         }
  2197.         else                                    // recover
  2198.         {
  2199.             DisposeIfHandle(dataHandle);
  2200.             fi.ReSignal();
  2201.         }
  2202.     }
  2203. }
  2204.  
  2205. //========================================================================================
  2206. // CLASS CTempDesc
  2207. //========================================================================================
  2208. #undef Inherited
  2209.  
  2210. //----------------------------------------------------------------------------------------
  2211. // CTempDesc::CTempDesc: 
  2212. //----------------------------------------------------------------------------------------
  2213. #pragma segment ConstructorRes
  2214.  
  2215. CTempDesc::CTempDesc()
  2216. {
  2217.     fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
  2218. }
  2219.  
  2220. //--------------------------------------------------------------------------------------------------
  2221. // CTempDesc::CTempDesc
  2222. //--------------------------------------------------------------------------------------------------
  2223. #pragma segment ConstructorRes
  2224.  
  2225. CTempDesc::CTempDesc(const AEDesc& theDesc) :
  2226.     CAEDesc(theDesc)
  2227. {
  2228.     fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
  2229. }
  2230.  
  2231. //--------------------------------------------------------------------------------------------------
  2232. // CTempDesc::CTempDesc
  2233. //--------------------------------------------------------------------------------------------------
  2234. #pragma segment ConstructorRes
  2235.  
  2236. CTempDesc::CTempDesc(DescType theType,
  2237.                      Handle theHandle) :
  2238.     CAEDesc(theType, theHandle)
  2239. {
  2240.     fFailInfo.SetCleanupProc(CTempDesc::CallCleanup, this);
  2241. }
  2242.  
  2243. //--------------------------------------------------------------------------------------------------
  2244. // CTempDesc::~CTempDesc: 
  2245. //--------------------------------------------------------------------------------------------------
  2246. #pragma segment AEClose
  2247.  
  2248. CTempDesc::~CTempDesc()
  2249. {
  2250.     //Cleanup();
  2251.     if (fAEDesc.dataHandle)                        // optimization
  2252.         DisposeDesc();
  2253.  
  2254.     fAEDesc.descriptorType = typeNull;
  2255.     fAEDesc.dataHandle = NULL;
  2256.  
  2257.     fFailInfo.Success();
  2258. }
  2259.  
  2260. //--------------------------------------------------------------------------------------------------
  2261. // CTempDesc::Cleanup: 
  2262. //--------------------------------------------------------------------------------------------------
  2263. #pragma segment AEClose
  2264.  
  2265. void CTempDesc::Cleanup()
  2266. {
  2267.     if (fAEDesc.dataHandle)                        // optimization
  2268.         DisposeDesc();
  2269. }
  2270.  
  2271. //--------------------------------------------------------------------------------------------------
  2272. // CTempDesc::CallCleanup: 
  2273. //--------------------------------------------------------------------------------------------------
  2274. #pragma segment AEClose
  2275.  
  2276. /*static*/
  2277. void CTempDesc::CallCleanup(void* context)
  2278. {
  2279.     //((CTempDesc*) context)->Cleanup();
  2280.     ((CTempDesc *)context)->DisposeDesc();        // optimization
  2281. }
  2282.  
  2283. //----------------------------------------------------------------------------------------
  2284. // End of UAppleEvents.cp
  2285.  
  2286. #pragma segment Inline
  2287.  
  2288.